package com.tarena.lbs.content.service.impl;


import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.dao.repository.CategoryRepository;

import com.tarena.lbs.content.pojo.param.ArticleCategoryParam;
import com.tarena.lbs.content.pojo.po.ArticleCategoryPO;
import com.tarena.lbs.content.pojo.query.ArticleCategoryQuery;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import com.tarena.lbs.content.service.CategoryService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryRepository categoryRepository;
    @Override
    public PageResult<ArticleCategoryVO> pageList() {
        List<ArticleCategoryPO> pos = categoryRepository.list();
        PageResult<ArticleCategoryVO> page = new PageResult<>(10,1);
        page.setTotal(100L);
        page.setObjects(pos.stream().map(po->{
            ArticleCategoryVO vo=new ArticleCategoryVO();
            BeanUtils.copyProperties(po,vo);
            return vo;
        }).collect(Collectors.toList()));
        return page;
    }

    @Override
    public void addCategory(ArticleCategoryParam param) {
        log.info("新增分类{}",param);
        ArticleCategoryPO po=new ArticleCategoryPO();
        BeanUtils.copyProperties(param,po);
        po.setCreateTime(new Date());
        po.setUpdateTime(new Date());
        categoryRepository.insert(po);
    }

    @Override
    public void updateCategory(ArticleCategoryParam param) {
        log.info("修改文章分类{}",param);
        ArticleCategoryPO po=new ArticleCategoryPO();
        po.setCategoryName(param.getCategoryName());
        po.setSort(param.getSort());
        po.setCategoryStatus(param.getCategoryStatus());
        categoryRepository.updateCategory(po);
    }

    @Override
    public void deleteCategory(Integer id) {
        log.info("删除文章分类{}",id);
        categoryRepository.deleteCategory(id);
    }

    @Override
    public List<ArticleCategoryVO> getCategory(ArticleCategoryQuery query) {
        log.info("查询文章----service{}",query);

        List<ArticleCategoryVO> categoryVO = categoryRepository.getCategory(query);
        return categoryVO;
    }


}
